{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ipytree: Interactive tree view based on ipywidgets\n",
    "\n",
    "## https://github.com/QuantStack/ipytree/\n",
    "\n",
    "\n",
    "ipytree is a jupyter interactive widget library which provides a tree widget to the Jupyter notebook.\n",
    "\n",
    "- MIT Licensed\n",
    "\n",
    "**Installation:**\n",
    "\n",
    "```bash\n",
    "conda install -c conda-forge ipytree\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Create a tree"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from ipywidgets import Text, link\n",
    "from ipytree import Tree, Node"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tree = Tree()\n",
    "tree.add_node(Node('node1'))\n",
    "\n",
    "node2 = Node('node2')\n",
    "tree.add_node(node2)\n",
    "\n",
    "tree"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "node3 = Node('node3', disabled=True)\n",
    "node4 = Node('node4')\n",
    "node5 = Node('node5', [Node('1'), Node('2')])\n",
    "node2.add_node(node3)\n",
    "node2.add_node(node4)\n",
    "node2.add_node(node5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Change node name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "name = Text('node3')\n",
    "link((name, 'value'), (node3, 'name'))\n",
    "\n",
    "name"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Change node icon"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "node4.icon = 'archive'\n",
    "node4.icon_style = 'warning'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Handle click"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def handle_click(event):\n",
    "    event['owner'].name = 'I am selected!!!' if event['new'] else 'I am not selected...' \n",
    "\n",
    "node4.observe(handle_click, 'selected')\n",
    "node4.selected = True"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "widgets-tutorial",
   "language": "python",
   "name": "widgets-tutorial"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
